################# Import Data #################

all_data <- c("Data/Framing the Exit Experiment Raw Data.csv")
all_data <- lapply(all_data, read.csv) %>% bind_rows()

################# Data Cleaning #################

#Remove all incomplete entries
data <- all_data[(all_data$Finished == "1"),]
data <- data[(data$participate == "2"),]
data <- data[(data$DistributionChannel == "anonymous"),]
data <- data[(data$Progress == "100"),]
data <- data[(data$Finished == "1"),]
#data <- data[(data$Q5 == "1"),]
#data <- data[(data$Q_RecaptchaScore != "0.400000006"),]
data <- data[(data$Q2 != "1"),]

#Combine like variables
# Cooperate
data$cooperate <- paste0(data$Q12_1, data$Q23_1, data$Q34_1, data$Q56_1, data$Q67_1, data$Q78_1, data$Q89_1, data$Q111_1)

# Trust
data$trust <- paste0(data$Q12_2, data$Q23_2, data$Q34_2, data$Q56_2, data$Q67_2, data$Q78_2, data$Q89_2, data$Q111_2)

# Dislike Cultures
data$dislike_cultures <- paste0(data$Q12_3, data$Q23_3, data$Q34_3, data$Q56_3, data$Q67_3, data$Q78_3, data$Q89_3, data$Q111_3)

# Similar Cultures
data$similar_cultures <- paste0(data$Q12_4, data$Q23_4, data$Q34_4, data$Q56_4, data$Q67_4, data$Q78_4, data$Q89_4, data$Q111_4)

# Lived Culture
data$lived_culture <- paste0(data$Q12_5, data$Q23_5, data$Q34_5, data$Q56_5, data$Q67_5, data$Q78_5, data$Q89_5, data$Q111_5)

# Lifestyles
data$lifestyles <- paste0(data$Q12_6, data$Q23_6, data$Q34_6, data$Q56_6, data$Q67_6, data$Q78_6, data$Q89_6, data$Q111_6)

# Role Model
data$role_model <- paste0(data$Q12_7, data$Q23_7, data$Q34_7, data$Q56_7, data$Q67_7, data$Q78_7, data$Q89_7, data$Q111_7)

# Military Force
data$military_force <- paste0(data$Q13, data$Q24, data$Q35, data$Q57, data$Q68, data$Q79, data$Q90, data$Q101, data$Q112)

# Great Powers
data$great_powers <- paste0(data$Q14, data$Q25, data$Q36, data$Q47, data$Q58, data$Q69, data$Q80, data$Q91, data$Q102, data$Q113)

# Political Affiliation 
data$political_affiliation <- paste0(data$Q11, data$Q22, data$Q33, data$Q55, data$Q66, data$Q77, data$Q88, data$Q99, data$Q110)

# Religion
data$religion <- paste0(data$Q15, data$Q26, data$Q37, data$Q48, data$Q59, data$Q70, data$Q81, data$Q92, data$Q103, data$Q114)

# Religious Importance
data$religious_importance <- paste0(data$Q16, data$Q27, data$Q38, data$Q49, data$Q60, data$Q71, data$Q82, data$Q93, data$Q104, data$Q115)

# Withdrawal
data$withdrawal <- paste0(data$Q131, data$Q132, data$Q133, data$Q134)

#Rename Variables
names(data)[names(data) == 'StartDate'] <- 'start'
names(data)[names(data) == 'EndDate'] <- 'end'
names(data)[names(data) == 'Duration..in.seconds.'] <- 'duration'
names(data)[names(data) == 'RecordedDate'] <- 'recorded'
names(data)[names(data) == 'ResponseId'] <- 'id'
names(data)[names(data) == 'Q_RecaptchaScore'] <- 'captcha'
names(data)[names(data) == 'Respondent.Group2'] <- 'condition'
names(data)[names(data) == 'Q1'] <- 'gender'
names(data)[names(data) == 'Q2'] <- 'age'
names(data)[names(data) == 'Q3'] <- 'race'
names(data)[names(data) == 'Q4'] <- 'education'
names(data)[names(data) == 'Q122'] <- 'income'
names(data)[names(data) == 'Q123'] <- 'state'
names(data)[names(data) == 'Q124'] <- 'military'
names(data)[names(data) == 'Q125'] <- 'military_years'
names(data)[names(data) == 'Q126'] <- 'military_branch'
names(data)[names(data) == 'Q127'] <- 'military_specialforces'
names(data)[names(data) == 'Q128'] <- 'military_enlisted'
names(data)[names(data) == 'Q129'] <- 'military_deployed'
names(data)[names(data) == 'Q130'] <- 'military_combat'

#Recode Factors
data$condition_recode <- data$condition
data$condition_recode <- recode_factor(data$condition_recode,"Condition #1, Treatment" = "Control_Reduce")
data$condition_recode <- recode_factor(data$condition_recode,"Condition #2, Treatment" = "Control_Increase")
data$condition_recode <- recode_factor(data$condition_recode,"Condition #3, Treatment" = "Taliban_Reduce")
data$condition_recode <- recode_factor(data$condition_recode,"Condition #4, Treatment" = "Taliban_Increase")

data$condition <- data$condition_recode
data$condition <- recode_factor(data$condition,"Control_Reduce" = "1")
data$condition <- recode_factor(data$condition,"Control_Increase" = "2")
data$condition <- recode_factor(data$condition,"Taliban_Reduce" = "3")
data$condition <- recode_factor(data$condition,"Taliban_Increase" = "4")

data$withdrawal_recode <- data$withdrawal
data$withdrawal_recode <- recode_factor(data$withdrawal_recode,"1" = "least")
data$withdrawal_recode <- recode_factor(data$withdrawal_recode,"2" = "middle")
data$withdrawal_recode <- recode_factor(data$withdrawal_recode,"3" = "most")

data$military_recode <- data$military
data$military_recode <- recode_factor(data$military_recode,"1" = "yes")
data$military_recode <- recode_factor(data$military_recode,"2" = "no")

# Taliban
t1 <- data[(data$condition_recode == "Taliban_Reduce"),]
t2 <- data[(data$condition_recode == "Taliban_Increase"),]
t <- bind_rows(t1, t2)
t$taliban_recode <- "taliban"
c1 <- data[(data$condition_recode == "Control_Reduce"),]
c2 <- data[(data$condition_recode == "Control_Increase"),]
c <- bind_rows(c1, c2)
c$taliban_recode <- "control"
data <- bind_rows(t,c)
rm(t1, t2, t, c1, c2, c)

# Framing
r1 <- data[(data$condition_recode == "Taliban_Reduce"),]
r2 <- data[(data$condition_recode == "Control_Reduce"),]
r <- bind_rows(r1, r2)
r$framing_recode <- "reduce"
i1 <- data[(data$condition_recode == "Taliban_Increase"),]
i2 <- data[(data$condition_recode == "Control_Increase"),]
i <- bind_rows(i1, i2)
i$framing_recode <- "increase"
data <- bind_rows(r,i)
rm(r1, r2, r, i1, i2, i)

data$taliban <- data$taliban_recode
data$taliban <- recode_factor(data$taliban,"control" = "1")
data$taliban <- recode_factor(data$taliban,"taliban" = "2")

data$framing <- data$framing_recode
data$framing <- recode_factor(data$framing,"reduce" = "1")
data$framing <- recode_factor(data$framing,"increase" = "2")

# Create Culture Panel Average Variable
data$cooperate_n <- as.numeric(data$cooperate)
data$trust_n <- as.numeric(data$trust)
data$dislike_cultures_n <- as.numeric(data$dislike_cultures)
data$similar_cultures_n <- as.numeric(data$similar_cultures)
data$lived_culture_n <- as.numeric(data$lived_culture)
data$lifestyles_n <- as.numeric(data$lifestyles)
data$role_model_n <- as.numeric(data$role_model)

data$culture_panel <- ((data$cooperate_n+data$trust_n+data$dislike_cultures_n+data$similar_cultures_n+data$lived_culture_n+data$lifestyles_n+data$role_model_n)/7)
data$culture_panel[is.na(data$culture_panel)] <- ""

f1 <- data[(data$framing == "1"),]
f1$withdraw_yn <- ifelse(f1$withdrawal == "1", 1, 0)
f1$staycourse_yn <- ifelse(f1$withdrawal == "3", 1, 0)

f2 <- data[(data$framing == "2"),]
f2$withdraw_yn <- ifelse(f2$withdrawal == "1", 1, 0)
f2$staycourse_yn <- ifelse(f2$withdrawal == "2", 1, 0)

data <- bind_rows(f1,f2)
rm(f1,f2)

data$liberal <- "0"
data$liberal <- ifelse(data$political_affiliation == "1", 1, data$liberal)
data$liberal <- ifelse(data$political_affiliation == "2", 1, data$liberal)
data$liberal <- ifelse(data$political_affiliation == "3", 1, data$liberal)


# Create Clean Dataset
data <- data %>% dplyr::select(id, condition, condition_recode, taliban, taliban_recode, framing, framing_recode,
                        withdrawal,withdraw_yn, staycourse_yn,
                        cooperate, 
                        trust, 
                        military_force, great_powers,
                        political_affiliation, liberal,
                        dislike_cultures, similar_cultures, lived_culture, lifestyles, role_model, culture_panel,
                        religion, religious_importance,
                        gender, age, race, education, income, state,
                        military, military_years, military_branch, military_specialforces, military_enlisted, military_deployed, military_combat,
                        start, end, duration, recorded, captcha)

write.csv(data,"Data/Framing the Exit Clean Data.csv",row.names=FALSE)


# Create Condensed Dataset
data_condensed <- data %>% dplyr::select(id, condition, condition_recode, taliban, taliban_recode, framing, framing_recode,
                        withdrawal, withdraw_yn, staycourse_yn,
                        cooperate, 
                        trust,
                        liberal,
                        culture_panel,
                        military_force, great_powers,
                        political_affiliation,
                        dislike_cultures, similar_cultures, lived_culture, lifestyles, role_model,
                        religion, religious_importance, military)

write.csv(data_condensed,"Data/Framing the Exit Condensed Data.csv",row.names=FALSE)

# Recode Variables
data$gender <- recode_factor(data$gender,"1" = "Male")
data$gender <- recode_factor(data$gender,"2" = "Female")
data$gender <- recode_factor(data$gender,"3" = "Other")

data$age <- recode_factor(data$age,"1" = "Under 18")
data$age <- recode_factor(data$age,"2" = "19-25")
data$age <- recode_factor(data$age,"3" = "26-35")
data$age <- recode_factor(data$age,"4" = "36-45")
data$age <- recode_factor(data$age,"5" = "46-55")
data$age <- recode_factor(data$age,"6" = "56-65")
data$age <- recode_factor(data$age,"7" = "Over 66")

data$race <- recode_factor(data$race,"1" = "American Indian and Alaskan Native")
data$race <- recode_factor(data$race,"2" = "Asian")
data$race <- recode_factor(data$race,"3" = "Black")
data$race <- recode_factor(data$race,"4" = "Hispanic/Latino")
data$race <- recode_factor(data$race,"5" = "Native Hawaiian and Other Pacific Islander")
data$race <- recode_factor(data$race,"6" = "White, Non-Hispanic")
data$race <- recode_factor(data$race,"7" = "Other")

data$education <- recode_factor(data$education,"1" = "Some high school")
data$education <- recode_factor(data$education,"2" = "High school (diploma or GED)")
data$education <- recode_factor(data$education,"3" = "Some college, but no degree")
data$education <- recode_factor(data$education,"4" = "2-year college degree")
data$education <- recode_factor(data$education,"5" = "4-year college degree")
data$education <- recode_factor(data$education,"6" = "Advanced or professional degree")

data$political_affiliation <- recode_factor(data$political_affiliation,"1" = "Extremely Liberal")
data$political_affiliation <- recode_factor(data$political_affiliation,"2" = "Liberal")
data$political_affiliation <- recode_factor(data$political_affiliation,"3" = "Slightly Liberal")
data$political_affiliation <- recode_factor(data$political_affiliation,"4" = "Moderate, middle of the road")
data$political_affiliation <- recode_factor(data$political_affiliation,"5" = "Slightly Conservative")
data$political_affiliation <- recode_factor(data$political_affiliation,"6" = "Conservative")
data$political_affiliation <- recode_factor(data$political_affiliation,"7" = "Extremely Conservative")
data$political_affiliation <- recode_factor(data$political_affiliation,"8" = "I am not sure")

data$religion <- recode_factor(data$religion,"1" = "Protestant Christian")
data$religion <- recode_factor(data$religion,"2" = "Catholic")
data$religion <- recode_factor(data$religion,"3" = "Other Christian")
data$religion <- recode_factor(data$religion,"4" = "Jewish")
data$religion <- recode_factor(data$religion,"5" = "Muslim")
data$religion <- recode_factor(data$religion,"6" = "Buddhist")
data$religion <- recode_factor(data$religion,"7" = "Hindu")
data$religion <- recode_factor(data$religion,"8" = "Atheist")
data$religion <- recode_factor(data$religion,"9" = "No formal religious affiliation")
data$religion <- recode_factor(data$religion,"10" = "Other")

data$religious_importance <- recode_factor(data$religious_importance,"1" = "Very Unimportant")
data$religious_importance <- recode_factor(data$religious_importance,"2" = "Somewhat Unimportant")
data$religious_importance <- recode_factor(data$religious_importance,"3" = "Neutral")
data$religious_importance <- recode_factor(data$religious_importance,"4" = "Somewhat Important")
data$religious_importance <- recode_factor(data$religious_importance,"5" = "Very Important")

data$income <- recode_factor(data$income,"1" = "Less than $9,999")
data$income <- recode_factor(data$income,"2" = "$10,000 to $24,999")
data$income <- recode_factor(data$income,"3" = "$25,000 to $49,999")
data$income <- recode_factor(data$income,"4" = "$50,000 to $74,999")
data$income <- recode_factor(data$income,"5" = "$75,000 to $99,999")
data$income <- recode_factor(data$income,"6" = "$100,000 or more")

data$military <- recode_factor(data$military,"1" = "Served in the US military")
data$military <- recode_factor(data$military,"2" = "Did not serve in the US military")

data$cooperate <- recode_factor(data$cooperate,"1" = "Strongly Disagree")
data$cooperate <- recode_factor(data$cooperate,"2" = "Disagree")
data$cooperate <- recode_factor(data$cooperate,"3" = "Neither Agree Nor Disagree")
data$cooperate <- recode_factor(data$cooperate,"4" = "Agree")
data$cooperate <- recode_factor(data$cooperate,"5" = "Strongly Agree")

data$trust <- recode_factor(data$trust,"1" = "Strongly Disagree")
data$trust <- recode_factor(data$trust,"2" = "Disagree")
data$trust <- recode_factor(data$trust,"3" = "Neither Agree Nor Disagree")
data$trust <- recode_factor(data$trust,"4" = "Agree")
data$trust <- recode_factor(data$trust,"5" = "Strongly Agree")

data$military_force <- recode_factor(data$military_force,"1" = "Strongly Disagree")
data$military_force <- recode_factor(data$military_force,"2" = "Disagree")
data$military_force <- recode_factor(data$military_force,"3" = "Neither Agree Nor Disagree")
data$military_force <- recode_factor(data$military_force,"4" = "Agree")
data$military_force <- recode_factor(data$military_force,"5" = "Strongly Agree")

data$great_powers <- recode_factor(data$great_powers,"1" = "Strongly Disagree")
data$great_powers <- recode_factor(data$great_powers,"2" = "Disagree")
data$great_powers <- recode_factor(data$great_powers,"3" = "Neither Agree Nor Disagree")
data$great_powers <- recode_factor(data$great_powers,"4" = "Agree")
data$great_powers <- recode_factor(data$great_powers,"5" = "Strongly Agree")

data$dislike_cultures <- recode_factor(data$dislike_cultures,"1" = "Strongly Disagree")
data$dislike_cultures <- recode_factor(data$dislike_cultures,"2" = "Disagree")
data$dislike_cultures <- recode_factor(data$dislike_cultures,"3" = "Neither Agree Nor Disagree")
data$dislike_cultures <- recode_factor(data$dislike_cultures,"4" = "Agree")
data$dislike_cultures <- recode_factor(data$dislike_cultures,"5" = "Strongly Agree")

data$similar_cultures <- recode_factor(data$similar_cultures,"1" = "Strongly Disagree")
data$similar_cultures <- recode_factor(data$similar_cultures,"2" = "Disagree")
data$similar_cultures <- recode_factor(data$similar_cultures,"3" = "Neither Agree Nor Disagree")
data$similar_cultures <- recode_factor(data$similar_cultures,"4" = "Agree")
data$similar_cultures <- recode_factor(data$similar_cultures,"5" = "Strongly Agree")

data$lived_culture <- recode_factor(data$lived_culture,"1" = "Strongly Disagree")
data$lived_culture <- recode_factor(data$lived_culture,"2" = "Disagree")
data$lived_culture <- recode_factor(data$lived_culture,"3" = "Neither Agree Nor Disagree")
data$lived_culture <- recode_factor(data$lived_culture,"4" = "Agree")
data$lived_culture <- recode_factor(data$lived_culture,"5" = "Strongly Agree")

data$lifestyles <- recode_factor(data$lifestyles,"1" = "Strongly Disagree")
data$lifestyles <- recode_factor(data$lifestyles,"2" = "Disagree")
data$lifestyles <- recode_factor(data$lifestyles,"3" = "Neither Agree Nor Disagree")
data$lifestyles <- recode_factor(data$lifestyles,"4" = "Agree")
data$lifestyles <- recode_factor(data$lifestyles,"5" = "Strongly Agree")

data$role_model <- recode_factor(data$role_model,"1" = "Strongly Disagree")
data$role_model <- recode_factor(data$role_model,"2" = "Disagree")
data$role_model <- recode_factor(data$role_model,"3" = "Neither Agree Nor Disagree")
data$role_model <- recode_factor(data$role_model,"4" = "Agree")
data$role_model <- recode_factor(data$role_model,"5" = "Strongly Agree")

data$withdrawal <- recode_factor(data$withdrawal,"1" = "least")
data$withdrawal <- recode_factor(data$withdrawal,"2" = "middle")
data$withdrawal <- recode_factor(data$withdrawal,"3" = "most")

data_recoded <- data %>% dplyr::select(id, condition_recode, taliban_recode, framing_recode,
                                  withdrawal, withdraw_yn, staycourse_yn,
                                  cooperate,
                                  trust,
                                  military_force, great_powers,
                                  political_affiliation,
                                  dislike_cultures, similar_cultures, lived_culture, lifestyles, role_model,
                                  religion, religious_importance,
                                  gender, age, race, education, income, military, state)

write.csv(data_recoded,"Data/Framing the Exit Recoded Data.csv",row.names=FALSE)


################# Import Data #################
rm(list=ls())

data <- c("Data/Framing the Exit Clean Data.csv")
data <- lapply(data, read.csv) %>% bind_rows()

data_recoded <- c("Data/Framing the Exit Recoded Data.csv")
data_recoded <- lapply(data_recoded, read.csv) %>% bind_rows()



################# Summary Statistics #################
# df <- data %>%
#   group_by(political_affiliation) %>%
#   summarise(counts = n())
# df
# 
# ggplot(df, aes(counts))+
#   geom_bar(fill = "#0073C2FF")

# data %>% 
#   group_by(liberal) %>% 
#   tally()

